<?php
 /*
 * 图片微缩类
 * author chendan 2011.09.01.
 */
class Thumbnail {
	protected $_original;
	//原始宽度
	protected $_originalwidth;
	//原始高度
	protected $_originalheight;
	//缩微宽度
	protected $_thumbwidth;
	//缩微高度
	protected $_thumbheight;
	protected $_maxsize = 120;
	protected $_canprocess = false;
	protected $_imagetype;
	protected $_destination;
	protected $_name;
	protected $_suffix = "_thb";
	protected $_messages = array();
	protected $_filename;

	public function __construct($image) {
		if(is_file($image) && is_readable($image)){
			$details = getimagesize($image);
		}else{
			$details = null;
			$this->_messages[] = "Can not open $image";
		}
		if(is_array($details)){
			$this->_original = $image;
			$this->_originalwidth = $details[0];
			$this->_originalheight = $details[1];
			$this->checkType($details['mime']);
		}else{
			$this->_messages[] = "$image does not appear to be an image"; 
		}
	}

	protected function checkType($mime) {
		$mimetypes = array('image/jpeg','image/jpg','image/png','image/gif');
		if(in_array($mime,$mimetypes)){
			$this->_canprocess = true;
			$this->_imagetype = substr($mime,6);
		}
	}

	public function setDestination($destination) {
		if(is_dir($destination) && is_writable($destination)){
			$last = substr($destination,-1);
			if($last == '/' || $last =="\\"){
				$this->_destination = $destination;
			}else{
				$this->_destination = $destination.DIRECTORY_SEPARATOR;
			}
		}else{
			$this->_messages[] = "Cannot write to $destination";
		}
	}

	public function setMaxSize($size) {
		if(is_numeric($size) && $size > 0){
			$this->_maxsize = $size;
		}else{
			$this->_canprocess = false;
			$this->_messages[] = "The value for setMaxSize() must be a positive number";
		}
	}

	public function setSuffix($suffix) {
		if(preg_match('/^\w+$/',$suffix)){
			if(strpos($suffix,'_')!==0){
				$this->_suffix = '_'.$suffix;
			}else{
				$this->_suffix = $suffix;
			}
		}else{
			$this->_suffix = '';
		}
	}

	protected function calculateSize($width,$height) {
		if($width<$this->_maxsize && $height < $this->_maxsize){
			$ratio = 1;
		}elseif($width>$height){
			$ratio = $this->_maxsize / $width;
		}else{
			$ratio = $this->_maxsize / $height;
		}
		$this->_thumbwidth = round($width * $ratio);
		$this->_thumbheight = round($height * $ratio);
	}

	protected function getName() {
		$extensions = array('/\.jpeg$/i','/\.jpg$/i','/\.png$/i','/\.gif$/i');
		$this->_name = preg_replace($extensions,'',basename($this->_original));
	}

	public function create() {
		if($this->_canprocess && $this->_originalwidth != 0){
			$this->calculateSize($this->_originalwidth,$this->_originalheight);
			$this->getName();
			$this->createThumbnail();
		}elseif($this->_originalwidth == 0){
			$this->_messages[] = "Can not determine size of ".$this->_original;
		}
	}
	protected function createImageResource() {
		if($this->_imagetype == 'jpeg'){
			return imagecreatefromjpeg($this->_original);
		}elseif($this->_imagetype == 'png'){
			return imagecreatefrompng($this->_original);
		}elseif($this->_imagetype == 'gif'){
			return imagecreatefromgif($this->_original);
		}
	}
	protected function createThumbnail() {
		$resource = $this->createImageResource();
		$thumb = imagecreatetruecolor($this->_thumbwidth,$this->_thumbheight);
		imagecopyresampled($thumb,$resource,0,0,0,0,$this->_thumbwidth,$this->_thumbheight,$this->_originalwidth,$this->_originalheight);
		$newname = $this->_name.$this->_suffix;
		if($this->_imagetype == 'jpeg'){
			$newname .= '.jpg';
			$success = imagejpeg($thumb,$this->_destination.$newname,100);
		}elseif($this->_imagetype == 'png'){
			$newname .= '.png';
			$success = imagepng($thumb,$this->_destination.$newname,0);
		}elseif($this->_imagetype == 'gif'){
			$newname .= '.gif';
			$success = imagegif($thumb,$this->_destination.$newname);
		}
		if($success){
			$this->_filename = $newname;
			$this->_messages[] = "$newname 创建成功.";
		}else{
			$this->_messages[] = basename($this->_original)."不能创建缩微图";
		}
		imagedestroy($resource);
		imagedestroy($thumb);
	}

	public function getMessages() {
		return $this->_messages;
	}
	
	public function getFilename() {
		return $this->_filename;
	}
}

?>